package com.elinkway.infinitemovies.selfdata.dao;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmModel;
import io.realm.RealmResults;

/**
 * Created by zhaopeipei on 2017/7/9.
 */
public class BaseRealmDao<T extends RealmModel> {

    public Realm myBaseDaoRealm;

    // 默认调用共同的库
    public BaseRealmDao() {
        myBaseDaoRealm = RealmDaoManager.getInstance();
    }

    // 参数不同会创建不同的库
    public BaseRealmDao(String dbName) {
        myBaseDaoRealm = RealmDaoManager.getInstance(dbName);
    }

    public void delete(Class<T> clazz) {
        myBaseDaoRealm.delete(clazz);
    }

    public void deleteAll() {
        myBaseDaoRealm.deleteAll();
    }

    public List<T> queryAll(Class<T> clazz) {
        RealmResults<T> records = myBaseDaoRealm.where(clazz).findAll();
        /**
         * 对查询结果，按Id进行排序，只能对查询结果进行排序
         */
        //增序排列
//        records=records.sort("id");
        //降序排列
//        records=records.sort("id", Sort.DESCENDING);
        return myBaseDaoRealm.copyFromRealm(records);
    }

}
